﻿Imports System.Drawing.Drawing2D
Imports System.ComponentModel

Public Class EllipseButton
    Inherits Button

    Private m_startColor As Color = Color.Blue
    ' private Color costartColor=Color.White;
    Private m_endColor As Color = Color.GreenYellow
    '注视 
    <Description("设定渐变的起始色"), Category("Appearance")> _
    Public Property StartColor() As Color
        Get
            Return m_startColor
        End Get
        Set(ByVal value As Color)
            m_startColor = value
            RePaint()
        End Set
    End Property
    <Description("设定渐变的终止色"), Category("Appearance")> _
    Public Property EndColor() As Color
        Get
            Return m_endColor
        End Get
        Set(ByVal value As Color)
            m_endColor = value
            RePaint()
        End Set
    End Property

    Public Sub New()
        Me.Width = 100
        Me.Height = 100
    End Sub
    '绘制圆形区域
    Protected Overloads Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
        MyBase.OnPaint(pe)

        Dim g As Graphics = pe.Graphics
        g.Clear(Me.BackColor)
        Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
        Dim myBrush As New LinearGradientBrush(rect, m_startColor, m_endColor, LinearGradientMode.ForwardDiagonal)
        g.FillEllipse(myBrush, rect)
        myBrush.Dispose()
        Dim format As New StringFormat()
        format.LineAlignment = StringAlignment.Center
        format.Alignment = StringAlignment.Center


        g.DrawString(Me.Text, Font, New SolidBrush(Me.ForeColor), rect, format)
    End Sub
    '重新绘制圆形区域
    Private Sub RePaint()
        Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
        OnPaint(New PaintEventArgs(Me.CreateGraphics(), rect))
    End Sub
    Private Sub EllipseButton_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
        RePaint()
    End Sub
    Private Sub EllipseButton_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        RePaint()
    End Sub
End Class
